﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using bdnt_pets_system_Entity;
using System.Data;

namespace bdnt_pets_system_Bo.SystemManager
{
    public class DictInfoManager
    {
        //AccessHelperSQL accessHelper = AccessHelperSQL.GetInstance();
        AccessHelper accessHelper = AccessHelper.GetInstance();
        
        List<Tbl_DictInfo> ConvertTableToObjs(DataTable dt)
        {
            List<Tbl_DictInfo> returnlist = new List<Tbl_DictInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                returnlist.Add(new Tbl_DictInfo
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    DictName = dr["DictName"] == DBNull.Value ? "" : dr["DictName"].ToString(),
                    en_DictName = dr["en_DictName"] == DBNull.Value ? "" : dr["en_DictName"].ToString(),
                    DictType = dr["DictType"] == DBNull.Value ? 0 : Int32.Parse(dr["DictType"].ToString()),
                    DictValue = dr["DictValue"] == DBNull.Value ? "" : dr["DictValue"].ToString(),
                    PId = dr["PId"] == DBNull.Value ? 0 : Int32.Parse(dr["PId"].ToString()),
                    Sequence = dr["Sequence"] == DBNull.Value ? 0 : Int32.Parse(dr["Sequence"].ToString()),
                    Remark = dr["Remark"] == DBNull.Value ? "" : dr["Remark"].ToString()
                });
            }
            return returnlist;
        }

        public List<Tbl_DictInfo> GetObjListByCondition(string objName)
        {
            var sql = string.Format("select * from Tbl_DictInfo");
            if (!string.IsNullOrEmpty(objName))
                sql = string.Format("select * from Tbl_DictInfo where DictName like '%{0}%'", objName);
            var dt = accessHelper.ExcuteDataTable(sql);
            return ConvertTableToObjs(dt);
        }

        public List<Tbl_DictInfo> GetObjListByPid(int objId)
        {
            var sql = string.Format(@"select * from Tbl_DictInfo where Pid={0}",objId);           
            var dt = accessHelper.ExcuteDataTable(sql);
            return ConvertTableToObjs(dt);
        }
        
        public bool SaveObj(Tbl_DictInfo cuObj)
        {
            string sql = "";
            if (cuObj.Id == 0)
                sql = string.Format(@"INSERT INTO Tbl_DictInfo
           (DictName,DictValue,PId,DictType,Remark,Sequence,en_DictName)
            VALUES('{0}','{1}',{2},{3},'{4}',{5},'{6}')", cuObj.DictName, cuObj.DictValue, cuObj.PId, cuObj.DictType,
            cuObj.Remark, cuObj.Sequence, cuObj.en_DictName);

            else sql = string.Format(@"UPDATE Tbl_DictInfo
               SET DictName ='{0}'
                  ,DictValue = '{1}'
                  ,DictType = {2} 
                ,Remark = '{3}' 
                ,Sequence = {4} 
                ,en_DictName = '{5}' 
             WHERE Id={6}", cuObj.DictName, cuObj.DictValue, cuObj.DictType, cuObj.Remark, cuObj.Sequence, cuObj.en_DictName, cuObj.Id);

            return accessHelper.ExcultData(sql);
        }
        public Tbl_DictInfo GetObjById(int objId)
        {
            Tbl_DictInfo cur = new Tbl_DictInfo();
            var sql = string.Format(@"select * from Tbl_DictInfo where Id={0}", objId);
            var dt = accessHelper.ExcuteDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                cur = new Tbl_DictInfo
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    DictName = dr["DictName"] == DBNull.Value ? "" : dr["DictName"].ToString(),
                    en_DictName = dr["en_DictName"] == DBNull.Value ? "" : dr["en_DictName"].ToString(),
                    DictType = dr["DictType"] == DBNull.Value ? 0 : Int32.Parse(dr["DictType"].ToString()),
                    DictValue = dr["DictValue"] == DBNull.Value ? "" : dr["DictValue"].ToString(),
                    PId = dr["PId"] == DBNull.Value ? 0 : Int32.Parse(dr["PId"].ToString()),
                    Sequence = dr["Sequence"] == DBNull.Value ? 0 : Int32.Parse(dr["Sequence"].ToString()),
                    Remark = dr["Remark"] == DBNull.Value ? "" : dr["Remark"].ToString()
                };
            }
            return cur;
        }

        public bool DeleteCuObj(Int32 objId)
        {
            string sql = string.Format(@"call DeleteDict({0})", objId);
            return accessHelper.ExcultProc(sql);
        }

    }
}
